﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using BLL;
using TrainingWorkshop.Model;
using System.Data;
using CommonUtility;

namespace Presentation
{
    
    public partial class CourseList : System.Web.UI.Page
    {
        //declare componnents in web page
        private ContentPlaceHolder mpContentPlaceHolder;
        private TextBox CourseName;
        private CheckBox Open;
        private CheckBox Close;
        private int CourseStatus = -1;
        private DropDownList TeacherName;
        private DropDownList CourseLevel;
        private GridView CourseListView;
        private static TrainingWorkshop.Model.Condition cdt;
        public static List<TrainingWorkshop.Model.CourseList> testList;
        /// <summary>
        /// Page Load
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            //get componnents in master page 
            mpContentPlaceHolder = (ContentPlaceHolder)Master.FindControl("MainContent");
            CourseName = (TextBox)mpContentPlaceHolder.FindControl("CourseName");
            TeacherName = (DropDownList)mpContentPlaceHolder.FindControl("TeacherName");
            CourseLevel = (DropDownList)mpContentPlaceHolder.FindControl("CourseLevel");
            Open = (CheckBox)mpContentPlaceHolder.FindControl("StatusOpen");
            Close = (CheckBox)mpContentPlaceHolder.FindControl("StatusClose");
            CourseListView = (GridView)mpContentPlaceHolder.FindControl("CourseListView");

            if (!Page.IsPostBack)
            {
                //bind teacher name in dropdownlist
                bindDropTeacherName();

                //bind Course Level in dropdownlist
                bindDropLevelName();

                //set default column to sort
                ViewState["SortOrder"] = "CourseId";
                ViewState["OrderDire"] = "ASC";
            }
        }

        /// <summary>
        /// bind LevelName in Dropdownlist
        /// </summary>
        private void bindDropLevelName()
        {
            BLL.LevelBU l = new BLL.LevelBU();

            this.CourseLevel.DataSource = l.GetLevels();
            this.CourseLevel.DataTextField = "LevelName";
            this.CourseLevel.DataValueField = "LevelId";
            this.CourseLevel.DataBind();
            this.CourseLevel.Items.Insert(0, "--Choose Level--");
            this.CourseLevel.SelectedIndex = 0;


        }

        /// <summary>
        /// bind TeacherName in DropdownList
        /// </summary>
        private void bindDropTeacherName()
        {
            BLL.TeacherBU t = new BLL.TeacherBU();


            this.TeacherName.DataSource = t.GetTeachers();
            this.TeacherName.DataTextField = "TeacherName";
            this.TeacherName.DataValueField = "TeacherId";
            this.TeacherName.DataBind();


            this.TeacherName.Items.Insert(0, "--Choose Teacher--");
            this.TeacherName.SelectedIndex = 0;
        }

        /// <summary>
        /// Search Button Click
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void SearchBtnClick(object sender, EventArgs e)
        {


            //bind input data into BLL.Condition
            if (Open.Checked && !Close.Checked)
            {

                CourseStatus = 1;
            }
            else if (Close.Checked && !Open.Checked)
            {
                CourseStatus = 0;
            }
            else
            {
                CourseStatus = -1;
            }
            string TeacherName = this.TeacherName.SelectedItem.Text.Trim();
            string LevelName = this.CourseLevel.SelectedItem.Text.Trim();
            string CourseNameInput = this.CourseName.Text.Trim();

            cdt = new TrainingWorkshop.Model.Condition(CourseNameInput, CourseStatus, TeacherName, LevelName);
            bindReport();
        }

        /// <summary>
        /// bind CourseList Report to Gridview
        /// </summary>
        private void bindReport()
        {
            //pass condition
            BLL.CourseSearch cs = new BLL.CourseSearch();

            cdt.OrderDire = ViewState["OrderDire"].ToString();
            cdt.SortExpression = ViewState["SortOrder"].ToString();

             testList = new List<TrainingWorkshop.Model.CourseList>();
            testList = cs.GenerateReport(cdt);

            //find no record ?
            if (testList != null)
            {
                if (testList.Count > 0)
                {
                    errMsg.Visible = false;
                }
                else
                {
                    errMsg.Visible = true;
                }
            }

            //sort list
            var type = (CompareType)Enum.Parse(typeof(CompareType), ViewState["SortOrder"].ToString().Trim());

            CourseListComparer courselistComparer = new CourseListComparer(type);

            testList.Sort(courselistComparer);
            if (ViewState["OrderDire"].ToString() == "Desc")
            {
                testList.Reverse();
            }
            //bind list
            CourseListView.DataSource = testList;
            CourseListView.DataBind();
        }

        /// <summary>
        /// Gridview Paging
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void CourseListView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            //before paging
            CourseListView.PageIndex = e.NewPageIndex;
            bindReport();
        }

        /// <summary>
        /// GridView Sorting
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void CourseList_Sorting(object sender, GridViewSortEventArgs e)
        {
            String sortExpression = e.SortExpression;
            if (ViewState["SortOrder"].ToString() == sortExpression)
            {
                if (ViewState["OrderDire"].ToString() == "Desc")
                {
                    ViewState["OrderDire"] = "ASC";
                }
                else
                {
                    ViewState["OrderDire"] = "Desc";
                }
            }
            else
            {
                ViewState["SortOrder"] = e.SortExpression;
            }
            bindReport();
        }

        protected void CourseListView_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
        protected void EditCourse(object sender, GridViewEditEventArgs e)
        {
           //getSelectedCourse 
           int i= e.NewEditIndex;
           TrainingWorkshop.Model.CourseList cl=testList[i];
           cl.CourseName = cl.CourseName.Trim();
           cl.TeacherName = cl.TeacherName.Trim();
           cl.LevelName = cl.LevelName.Trim();
            //pass to course editing page
           this.Session["SelectedCourse"] = cl;
            Response.Redirect("CourseUpdate.aspx");
        
        }

    }
}
